/**
 * 2017年5月7日
 */
package cn.edu.bjtu.entity.impl.local;

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.ReadWriteLock;

import javax.servlet.http.HttpServletRequest;

import cn.edu.bjtu.entity.Command;
import cn.edu.bjtu.entity.ExecuteContext;
import cn.edu.bjtu.entity.ExecuteResult;
import cn.edu.bjtu.entity.impl.BaseLocalContext;

/**
 * 简单小任务.一般不需要并发控制的写到这里
 * 这个类即是命令也是执行环境,并且返回自己的结果.
 * @author Alex
 *
 */
public abstract class SelfContainedTask<R> extends BaseLocalContext<R> implements Command{

	protected Map<String,Object> resultMap = new TreeMap<String,Object>();
	protected HttpServletRequest req = null;
	/**
	 * @param lock
	 * @param inputDir
	 * @param outputDir
	 */
	protected SelfContainedTask(ReadWriteLock lock, String inputDir, String outputDir) {
		super(lock, inputDir, outputDir);
	}

	public SelfContainedTask(HttpServletRequest req) {
		this(null,null,null);
		this.req =  req;
	}

	@Override
	public Map<String, Object> getResultMapForRender() {
		return resultMap;
	}

	@Override
	public ExecuteResult<?> execute(ExecuteContext ctx) {
		if(ctx == null)
			ctx = this;
		ctx.fire();
		return (ExecuteResult<?>) ctx;
	}

}
